﻿using System;
using Drill.Configuration;
using Drill.TestHelpers;
using Drill.TestSupport;
using NUnit.Framework;

namespace Drill.Core.MasterFactories
{
	[TestFixture]
	public class ConfigBasedMasterDependencyResolverFactoryTests
	{
		[Test]
		public void CanConstruct()
		{
			// Arrange
			IDependencyResolverConfiguration config = new DependencyResolverConfiguration();

			// Act
			IDependencyResolverFactory factory = new ConfigBasedMasterDependencyResolverFactory(config);

			// Assert
			Assert.That(factory, Is.Not.Null);
		}

		[ExpectedException(typeof(ArgumentNullException), ExpectedMessage = "config", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotConstructWithNullConfigArgument()
		{
			// Arrange
			IDependencyResolverConfiguration config = null;

			// Act
			// ReSharper disable ExpressionIsAlwaysNull
			// ReSharper disable ObjectCreationAsStatement
			new ConfigBasedMasterDependencyResolverFactory(config);
			// ReSharper restore ObjectCreationAsStatement
			// ReSharper restore ExpressionIsAlwaysNull

			// Assert
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}

		[Test]
		public void CannotCreateInstanceWithDisabledDependencyResolverConfigArgument()
		{
			// Arrange
			IDependencyResolverConfiguration config = new DependencyResolverConfiguration { Enabled = false };
			IDependencyResolverFactory factory = new ConfigBasedMasterDependencyResolverFactory(config);

			// Act
			IDependencyResolver dependencyResolver = factory.CreateDependencyResolver();

			// Assert
			Assert.That(dependencyResolver, Is.Null);
		}

		[Test]
		public void CanCreateInstanceWithDefaultDependencyResolver()
		{
			// Arrange
			IDependencyResolverConfiguration config = TestConfigs.CreateBasicDependencyResolverConfigurationWithUnityDrillBit();
			IDependencyResolverFactory factory = new ConfigBasedMasterDependencyResolverFactory(config);

			// Act
			IDependencyResolver dependencyResolver = factory.CreateDependencyResolver();

			// Assert
			Assert.That(dependencyResolver, Is.Not.Null);
		}

		[Test]
		public void CanCreateInstanceWithDependencyResolverFactory()
		{
			// Arrange
			DependencyResolverConfiguration config = TestConfigs.CreateBasicDependencyResolverConfigurationWithUnityDrillBit();
			IDependencyResolverFactory factory = new ConfigBasedMasterDependencyResolverFactory(config);
			config.Type = typeof(TestUnityDependencyResolverFactory).AssemblyQualifiedName;

			// Act
			IDependencyResolver dependencyResolver = factory.CreateDependencyResolver();

			// Assert
			Assert.That(dependencyResolver, Is.Not.Null);
		}

		[Test]
		public void CanCreateInstanceWithDefaultDependencyResolverAndDrillBitFactory()
		{
			// Arrange
			DependencyResolverConfiguration config = TestConfigs.CreateBasicDependencyResolverConfigurationWithUnityDrillBit();
			DrillBitConfiguration drillBitConfig =
				(DrillBitConfiguration)config.DrillBits[0];
			drillBitConfig.Type = typeof(TestUnityDependencyResolverFactory).AssemblyQualifiedName;
			IDependencyResolverFactory factory = new ConfigBasedMasterDependencyResolverFactory(config);

			// Act
			IDependencyResolver dependencyResolver = factory.CreateDependencyResolver();

			// Assert
			Assert.That(dependencyResolver, Is.Not.Null);
		}

		[Test]
		public void CanCreateInstanceWithDefaultDependencyResolverAndDrillBitFactoryAndRootPathProvider()
		{
			// Arrange
			DependencyResolverConfiguration config = TestConfigs.CreateBasicDependencyResolverConfigurationWithUnityProviderAndRootPathProvider();
			DrillBitConfiguration drillBitConfig =
				(DrillBitConfiguration)config.DrillBits[0];
			drillBitConfig.Type = typeof(TestUnityDependencyResolverFactory).AssemblyQualifiedName;
			IDependencyResolverFactory factory = new ConfigBasedMasterDependencyResolverFactory(config);

			// Act
			IDependencyResolver dependencyResolver = factory.CreateDependencyResolver();

			// Assert
			Assert.That(dependencyResolver, Is.Not.Null);
		}

		[Test]
		public void CanCreateInstanceWithDefaultDependencyResolverAndDrillBitFactoryWithoutRootPathProvidersWithoutSearchPaths()
		{
			// Arrange
			DependencyResolverConfiguration config = TestConfigs.CreateBasicDependencyResolverConfigurationWithUnityProviderWithoutRootPathProvidersWithoutSearchPaths();
			IDependencyResolverFactory factory = new ConfigBasedMasterDependencyResolverFactory(config);

			// Act
			IDependencyResolver dependencyResolver = factory.CreateDependencyResolver();

			// Assert
			Assert.That(dependencyResolver, Is.Not.Null);
		}
	}
}